// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso.weather.fx;

import zombie.core.Rand;
import zombie.core.SpriteRenderer;
import zombie.core.textures.Texture;

public class RainParticle extends WeatherParticle {
    private double angleRadians = 0.0;
    private float lastAngle = -1.0F;
    private float lastIntensity = -1.0F;
    protected float angleOffset = 0.0F;
    private float alphaMod = 0.0F;
    private float incarnateAlpha = 1.0F;
    private float life = 0.0F;
    private RainParticle.RenderPoints rp;
    private boolean angleUpdate = false;
    private float tmpAngle = 0.0F;

    public RainParticle(Texture texture, int height) {
        super(texture);
        if (height > 6) {
            this.bounds.setSize(Rand.Next(1, 2), height);
        } else {
            this.bounds.setSize(1, height);
        }

        this.oWidth = (float)this.bounds.getWidth();
        this.oHeight = (float)this.bounds.getHeight();
        this.recalcSizeOnZoom = true;
        this.zoomMultiW = 0.0F;
        this.zoomMultiH = 2.0F;
        this.setLife();
        this.rp = new RainParticle.RenderPoints();
        this.rp.setDimensions(this.oWidth, this.oHeight);
    }

    protected void setLife() {
        this.life = (float)Rand.Next(20, 60);
    }

    @Override
    public void update(float delta) {
        this.angleUpdate = false;
        if (this.updateZoomSize()) {
            this.rp.setDimensions(this.oWidth, this.oHeight);
            this.angleUpdate = true;
        }

        if (this.angleUpdate || this.lastAngle != IsoWeatherFX.instance.windAngle || this.lastIntensity != IsoWeatherFX.instance.windPrecipIntensity.value()) {
            this.tmpAngle = IsoWeatherFX.instance.windAngle + (this.angleOffset - this.angleOffset * 0.5F * IsoWeatherFX.instance.windPrecipIntensity.value());
            if (this.tmpAngle > 360.0F) {
                this.tmpAngle -= 360.0F;
            }

            if (this.tmpAngle < 0.0F) {
                this.tmpAngle += 360.0F;
            }

            this.angleRadians = Math.toRadians((double)this.tmpAngle);
            this.velocity.set((float)Math.cos(this.angleRadians) * this.speed, (float)Math.sin(this.angleRadians) * this.speed);
            this.lastAngle = IsoWeatherFX.instance.windAngle;
            this.lastIntensity = IsoWeatherFX.instance.windPrecipIntensity.value();
            this.angleUpdate = true;
        }

        this.position.x = this.position.x + this.velocity.x * (1.0F + IsoWeatherFX.instance.windSpeed * 0.1F) * delta;
        this.position.y = this.position.y + this.velocity.y * (1.0F + IsoWeatherFX.instance.windSpeed * 0.1F) * delta;
        this.life--;
        if (this.life < 0.0F) {
            this.setLife();
            this.incarnateAlpha = 0.0F;
            this.position.set((float)Rand.Next(0, this.parent.getWidth()), (float)Rand.Next(0, this.parent.getHeight()));
        }

        if (this.incarnateAlpha < 1.0F) {
            this.incarnateAlpha += 0.035F;
            if (this.incarnateAlpha > 1.0F) {
                this.incarnateAlpha = 1.0F;
            }
        }

        super.update(delta, false);
        this.bounds.setLocation((int)this.position.x, (int)this.position.y);
        if (this.angleUpdate) {
            this.tmpAngle += 90.0F;
            if (this.tmpAngle > 360.0F) {
                this.tmpAngle -= 360.0F;
            }

            if (this.tmpAngle < 0.0F) {
                this.tmpAngle += 360.0F;
            }

            this.angleRadians = Math.toRadians((double)this.tmpAngle);
            this.rp.rotate(this.angleRadians);
        }

        this.alphaMod = 1.0F - 0.2F * IsoWeatherFX.instance.windIntensity.value();
        this.renderAlpha = this.alpha * this.alphaMod * this.alphaFadeMod.value() * IsoWeatherFX.instance.indoorsAlphaMod.value() * this.incarnateAlpha;
        this.renderAlpha *= 0.55F;
        if (IsoWeatherFX.instance.playerIndoors) {
            this.renderAlpha *= 0.5F;
        }
    }

    @Override
    public void render(float offsetx, float offsety) {
        double double0 = (double)(offsetx + (float)this.bounds.getX());
        double double1 = (double)(offsety + (float)this.bounds.getY());
        SpriteRenderer.instance
            .render(
                this.texture,
                double0 + this.rp.getX(0),
                double1 + this.rp.getY(0),
                double0 + this.rp.getX(1),
                double1 + this.rp.getY(1),
                double0 + this.rp.getX(2),
                double1 + this.rp.getY(2),
                double0 + this.rp.getX(3),
                double1 + this.rp.getY(3),
                this.color.r,
                this.color.g,
                this.color.b,
                this.renderAlpha,
                null
            );
    }

    private class Point {
        private double origx;
        private double origy;
        private double x;
        private double y;

        public void setOrig(double double0, double double1) {
            this.origx = double0;
            this.origy = double1;
            this.x = double0;
            this.y = double1;
        }

        public void set(double double0, double double1) {
            this.x = double0;
            this.y = double1;
        }
    }

    private class RenderPoints {
        RainParticle.Point[] points = new RainParticle.Point[4];
        RainParticle.Point center = RainParticle.this.new Point();
        RainParticle.Point dim = RainParticle.this.new Point();

        public RenderPoints() {
            for (int _int = 0; _int < this.points.length; _int++) {
                this.points[_int] = RainParticle.this.new Point();
            }
        }

        public double getX(int _int) {
            return this.points[_int].x;
        }

        public double getY(int _int) {
            return this.points[_int].y;
        }

        public void setCenter(float float1, float float0) {
            this.center.set((double)float1, (double)float0);
        }

        public void setDimensions(float float1, float float0) {
            this.dim.set((double)float1, (double)float0);
            this.points[0].setOrig((double)(-float1 / 2.0F), (double)(-float0 / 2.0F));
            this.points[1].setOrig((double)(float1 / 2.0F), (double)(-float0 / 2.0F));
            this.points[2].setOrig((double)(float1 / 2.0F), (double)(float0 / 2.0F));
            this.points[3].setOrig((double)(-float1 / 2.0F), (double)(float0 / 2.0F));
        }

        public void rotate(double double1) {
            double double0 = Math.cos(double1);
            double double2 = Math.sin(double1);

            for (int _int = 0; _int < this.points.length; _int++) {
                this.points[_int].x = this.points[_int].origx * double0 - this.points[_int].origy * double2;
                this.points[_int].y = this.points[_int].origx * double2 + this.points[_int].origy * double0;
            }
        }
    }
}
